package com.jackchen.common.filter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.jackchen.common.i18n.UserLocaleCache;

/**
 * request生命周期监听器
 * 后台没接收到一个request的时候，从session中获得当前登陆用户的语言标识，
 * 待ModelString在做数据库国际化的时候使用
 * 
 * @author Administrator
 *
 */
public class UserLocaleRequestListener implements ServletRequestListener {

	@Override
	public void requestDestroyed(ServletRequestEvent event) {
		ServletRequest sr = event.getServletRequest();
		if (sr instanceof HttpServletRequest) {
			HttpSession session = ((HttpServletRequest) sr).getSession();
			Object langure = session.getAttribute(LoginFilter.SESSION_USER_LANGUAGE);
			if (langure != null) {
				UserLocaleCache.getInstance().reset();
			}
		}
	}

	@Override
	public void requestInitialized(ServletRequestEvent event) {
		ServletRequest sr = event.getServletRequest();
		if (sr instanceof HttpServletRequest) {
			HttpSession session = ((HttpServletRequest) sr).getSession();
			Object langure = session.getAttribute(LoginFilter.SESSION_USER_LANGUAGE);
			if (langure != null) {
				int languageFlag = Integer.valueOf(langure.toString());
				UserLocaleCache.getInstance().setLangFlag(languageFlag == 0 ? "cn":"en");
			}
		}
	}

}
